package org.whatever.aha.zjut.platform.mapper.project;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.whatever.aha.zjut.platform.entity.project.Project;
import org.whatever.aha.zjut.platform.vo.project.ProjectDetailVo;
import org.whatever.aha.zjut.platform.vo.project.ProjectStuRoughVo;
import java.util.List;
/**
 * @author Vc
 * @version 1.0
 * @Desc
 * @date 2021/09/14 23:51
 */
public interface ProjectMapper extends BaseMapper<Project> {
    @Select("<script>"
            +"select project_info.project_id,project_info.comp_id as comp_id,stage_id,comp_th,project_name,project_info.project_leader_id,project_leader_name,project_student_status,project_enter_next_stage"
            +" from "
            +"(select comp_id,comp_th from competition where comp_type = #{compType})comp_info"
            +" inner join "
            +"(select * from project where project_delete_status = 0 "
            +"<if test='projectStudentStatus != null'>"
            +"and project_student_status = #{projectStudentStatus}"
            +"</if>"
            +" )project_info "
            +"on project_info.comp_id = comp_info.comp_id"
            +" inner join "
            +"(select project_id from project_member where member_id = #{userId})user_project"
            +" on user_project.project_id = project_info.project_id"
            +" join "
            +"(select user_id,username as project_leader_name from user)user_info"
            +" on user_info.user_id = project_info.project_leader_id"
            +" order by project_student_status,project_create_time limit #{offset}, #{pageSize}"
            +"</script>")
    List<ProjectStuRoughVo> getProjectRoughPageable(@Param("userId") int userId, @Param("compType") int compType, @Param("projectStudentStatus") Integer projectStudentStatus, @Param("offset") int offset, @Param("pageSize") int pageSize);

    //@Select("select * from (select * from Project where project_id = #{projectId})project left join (select * from academy)aca on project.project_academy_id = aca.academy_id")
    @Select("select * from project where project_id = #{projectId} and project_delete_status = 0")
    @Results({
            @Result(column="project_id",property="projectId"),
            @Result(column="project_academy_id",property="academyId"),
            @Result(column="project_id",property="projectMembers",
                    many=@Many(
                            select="org.whatever.aha.zjut.platform.mapper.project.ProjectMemberMapper.getProjectMembersById"
                    )
            ),
            //@Result(column = "academy_name",property = "academyName"),
            @Result(column = "project_academy_id",property = "academyName",
                    one=@One(
                            select="org.whatever.aha.zjut.platform.mapper.AcademyMapper.getAcademyName"
                    ))
    })
    ProjectDetailVo getProjectDetailById(@Param("projectId") int projectId);

    @Select("<script>"
            +"select count(*) from project where project_academy_id = #{academyId} and project_delete_status = 0 and project_school_rec = 0 "
            + "and comp_id = #{compId} and stage_id = #{stageId} "
            +"<if test='isNormal != null'>"
            +"and project_province_rec = 0 "
            +"</if>"
            +"<choose>"
            +"<when test='projectSupposeSubmit != null'>"
            +"and project_suppose_submit = #{projectSupposeSubmit} "
            +"</when>"
            +"<otherwise>"
            +"<![CDATA[ and project_academy_status <> 1 ]]>"
            +"</otherwise>"
            +"</choose>"
            +"</script>")
    int getProjectNumByAcademyIdAndStatus(@Param("academyId")int academyId,
                                          @Param("projectSupposeSubmit") Integer projectSupposeSubmit,
                                          @Param("compId")int compId,
                                          @Param("stageId")int stageId,
                                          @Param("isNormal")Boolean isNormal);

    @Select("select count(*) from project where stage_id = #{stageId} and project_province_rec = 1 and project_academy_id = #{academyId} and project_delete_status = 0 ")
    int getProvinceProjectNum(@Param("stageId")int stageId, @Param("academyId")int academyId);

    @Select("select count(*) from project where stage_id = #{stageId} and project_award_id = 4 and project_academy_id = #{academyId} and project_delete_status = 0 ")
    int getAcademyThirdPrizeProjectNum(@Param("stageId")int stageId, @Param("academyId")int academyId);

    @Select("<script>"
            +"SELECT project_info.* FROM (SELECT comp_id FROM competition WHERE comp_status = 3 and comp_year = #{compYear} and comp_type = #{compType}) comp_info"
            +" INNER JOIN "
//            +"(SELECT project_id,comp_id,project_name,project_leader_id,project_create_time,project_tag,project_group FROM project WHERE project_delete_status = 0"
            +"(SELECT * FROM project WHERE project_delete_status = 0 and stage_id = #{stageId}"
            +"<if test='academyId != null'>"
            +"and project_academy_id = #{academyId}"
            +"</if>"
            +") project_info "
            + "ON comp_info.comp_id = project_info.comp_id "
            + "ORDER BY project_info.project_award_id"
            +"</script>")
    List<Project> selectHistory(@Param("compYear")int compYear, @Param("compType")int compType, @Param("academyId")Integer academyId,@Param("stageId") int stageId);

    @Select("SELECT count(*) FROM project WHERE project_delete_status = 0 AND stage_id = #{stageId} AND project_school_rec = 1 AND project_school_status = 4")
    int getSchoolAlreadyRecNum(@Param("stageId")int stageId);

    @Select("SELECT count(*) FROM project WHERE project_delete_status = 0 AND stage_id = #{stageId} AND project_province_rec = 1 AND project_school_status = 4")
    Integer getDirectProvinceAlreadyRecNum(@Param("stageId")int stageId);

}
